package com.ruoyi.web.controller.threeViolation;

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.github.pagehelper.PageHelper;
import com.ruoyi.common.utils.pengjie.SnowflakeIdUtils;
import com.ruoyi.threeViolation.dto.ViolationExamineDto;
import com.ruoyi.threeViolation.dto.ViolationRegisterDto;
import com.ruoyi.threeViolation.vo.ViolationExamineVo;
import com.ruoyi.threeViolation.vo.ViolationRegisterVo;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.threeViolation.domain.ViolationRegister;
import com.ruoyi.threeViolation.service.IViolationRegisterService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 三违登记记录Controller
 *
 * @author lth
 * @date 2023-12-18
 */
@RestController
@RequestMapping("/threeViolation/register")
public class ViolationRegisterController extends BaseController
{
    @Autowired
    private IViolationRegisterService violationRegisterService;

    /**
     * 查询三违登记记录列表
     */
    @PreAuthorize("@ss.hasPermi('threeViolation:register:list')")
    @GetMapping("/list")
    public TableDataInfo list(ViolationRegister violationRegister)
    {
        startPage();
        List<ViolationRegister> list = violationRegisterService.selectViolationRegisterList(violationRegister);
        return getDataTable(list);
    }

    /**
     * 导出三违登记记录列表
     */
    @PreAuthorize("@ss.hasPermi('threeViolation:register:export')")
    @Log(title = "三违登记记录", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, ViolationRegister violationRegister)
    {
        List<ViolationRegister> list = violationRegisterService.selectViolationRegisterList(violationRegister);
        ExcelUtil<ViolationRegister> util = new ExcelUtil<ViolationRegister>(ViolationRegister.class);
        util.exportExcel(response, list, "三违登记记录数据");
    }

    /**
     * 获取三违登记记录详细信息
     */
    @PreAuthorize("@ss.hasPermi('threeViolation:register:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") String id)
    {
        return success(violationRegisterService.selectViolationRegisterById(id));
    }

    /**
     * 新增三违登记记录
     */
    @PreAuthorize("@ss.hasPermi('threeViolation:register:add')")
    @Log(title = "三违登记记录", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody ViolationRegister violationRegister)
    {
        long id = new SnowflakeIdUtils(5, 8).nextId();
        violationRegister.setId(String.valueOf(id));
        String s = "SW";
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyyMMdd");
        String currentDate = LocalDate.now().format(formatter);
        String code = s + currentDate;
        violationRegister.setVolationCode(code);
        //volation_dept_id   假数据
        violationRegister.setVolationDeptId("2120d4df123b483aa2a7516a1fc53b80");
        violationRegister.setVolationUser("1712296312083378178");
        return toAjax(violationRegisterService.insertViolationRegister(violationRegister));
    }

    /**
     * 修改三违登记记录
     */
    @PreAuthorize("@ss.hasPermi('threeViolation:register:edit')")
    @Log(title = "三违登记记录", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody ViolationRegister violationRegister)
    {
        return toAjax(violationRegisterService.updateViolationRegister(violationRegister));
    }

    /**
     * 删除三违登记记录
     */
    @PreAuthorize("@ss.hasPermi('threeViolation:register:remove')")
    @Log(title = "三违登记记录", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable String[] ids)
    {
        return toAjax(violationRegisterService.deleteViolationRegisterByIds(ids));
    }


    /**
     * 查询三违审核列表
     */
    @PreAuthorize("@ss.hasPermi('threeViolation:register:query')")
    @GetMapping("/auditList")
    public TableDataInfo auditList(ViolationRegisterVo vo)
    {
        PageHelper.startPage(vo.getPageNum(),vo.getPageSize());
        List<ViolationRegisterDto> list = violationRegisterService.auditList(vo);
        return getDataTable(list);
    }


    /**
     * 三违台账列表
     */
    @PreAuthorize("@ss.hasPermi('threeViolation:register:list')")
    @GetMapping("/accountList")
    public TableDataInfo accountList(ViolationRegisterVo vo)
    {
        PageHelper.startPage(vo.getPageNum(),vo.getPageSize());
        List<ViolationRegisterDto> list = violationRegisterService.auditList(vo);
        return getDataTable(list);
    }
}
